from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
from nltk.translate.meteor_score import single_meteor_score
import numpy as np


def batch_bleu(hypotheses, references, smooth_method=3, n=4, average=True):
    ' expect tokenized inputs '
    assert len(hypotheses) == len(references)
    cc = SmoothingFunction()
    smooth = getattr(cc, 'method' + str(smooth_method))
    weights = [1. / n] * n
    scores = [sentence_bleu([ref], hyp, weights, smoothing_function=smooth) for hyp, ref in zip(hypotheses, references)]
    return np.mean(scores) if average else scores
